const express = require('express');
const app = express();
var bodyParser = require('body-parser');
const path=require('path')
const fs=require('fs')
const resolve=(p)=>path.resolve(__dirname,p)
//设置跨域访问
app.use('/images',express.static(resolve('./images')))
app.all('*', function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By", ' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	next();
});

//写个接口123
app.get('/api/:viewname?', function(req, res) {
	var result=JSON.parse(fs.readFileSync(resolve("./api/"+req.params.viewname+".json")))
	res.status(200)
	res.json(result)
});
//app.post('/api/:viewname?',bodyParser.urlencoded({ extended: false }),function(req, res){
//	console.log(req.body)
//	var result=JSON.parse(fs.readFileSync(resolve("./api/"+req.params.viewname+".json")))
//	result.data.splice(parseInt(req.body.id),1)
//	fs.writeFile(resolve("./api/"+req.params.viewname+".json"),JSON.stringify(result))
//	res.json(result)
//})
//app.post('/api/:viewname?',bodyParser.urlencoded({ extended: false }),function(req, res){
//	var result=JSON.parse(fs.readFileSync(resolve("./api/"+req.params.viewname+".json")))
//	if(req.params.viewname=='list'){
//		req.body.type
//	}
//	
//	var arr=[];
//	for(var key in result.data){
//		
//	}
//	result.data.
//	fs.writeFile(resolve("./api/"+req.params.viewname+".json"),JSON.stringify(result))
//	res.json(result)
//})
app.post('/api/list',bodyParser.urlencoded({ extended: false }),function(req, res){
	var arr=[];
	var result=JSON.parse(fs.readFileSync(resolve("./api/list.json")))
	for(var key in result.data){
		if(result.data[key].type==req.body.type){
			arr.push(result.data[key])
		}
	}
	res.json({
		data:arr
	})
})

module.exports = app;